<?php
/**
 * Created by PhpStorm.
 * User: mamingwang
 * Date: 2019/3/5
 * Time: 9:56
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Xinxi extends Controller
{
    //评论导出
    public function daopinglun()
    {
        $data=db('evalute')
            ->alias('ev')
            ->join('f_message me','me.mid=ev.message_id')
            ->join('f_user us','us.uid=me.uid')
            ->field('ev.*,me.uid,us.uname')
            ->select();
        dump($data);die;
        for($i=0;$i<count($data);$i++){
            $as=db('user')->where('uid',$data[$i]['pjid'])->field('nickname,uname,uid')->find();
            if($as['uname']==""){
               $data[$i]['pjid']=$as['nickname'];
            }
            else{
            	$data[$i]['pjid']=$as['uname'];
            }
            $data[$i]['uuid']=$as['uid'];
        }
        for($i=0;$i<count($data);$i++){
            $as=db('user')->where('uid',$data[$i]['bpid'] )->field('nickname,uname,uid')->find();
           if($as['uname']==""){
           		 $data[$i]['bpid']=$as['nickname'];
           }
           else{
           		 $data[$i]['bpid']=$as['uname'];
           }
            $data[$i]['usid']=$as['uid'];
        }
        for($i=0;$i<count($data);$i++){
            $as=db('message')->where('mid',$data[$i]['message_id'] )->field('uid')->find();
            $ass=db('user')->where('uid',$as['uid'])->field('uname,nickname')->find();
            if($ass['uname']==""){
            	  $data[$i]['mname']=$ass['nickname'];
            }else{
            	 $data[$i]['mname']=$ass['uname'];
            }
           
        }
        Vendor('phpexcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('phpexcel.PHPExcel.Worksheet.Drawing');
        Vendor('phpexcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F");
        $arrHeader =  array('序号','评论ID','评论内容','信息ID','发布人','评论时间');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        $i=1;
        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$i++);
            $objActSheet->setCellValue('B'.$k, $v['evalute_id']);
            // // 图片生成
            // $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            // $objDrawing[$k]->setPath('public/static/admin/images/profile_small.jpg');
            // // 设置宽度高度
            // $objDrawing[$k]->setHeight(40);//照片高度
            // $objDrawing[$k]->setWidth(40); //照片宽度
            // /*设置图片要插入的单元格*/
            // $objDrawing[$k]->setCoordinates('C'.$k);
            // // 图片偏移距离
            // $objDrawing[$k]->setOffsetX(30);
            // $objDrawing[$k]->setOffsetY(12);
            // $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
            // 表格内容
            if($v['bpid']=="")
            {$v['txt']=$v['pjid'].':'.$v['txt'];}
            else
            {$v['txt']=$v['pjid']."回复".$v['bpid'].":".$v['txt'];}
            $objActSheet->setCellValue('C'.$k, $v['txt']);
            $objActSheet->setCellValue('D'.$k, $v['message_id']);
            $objActSheet->setCellValue('E'.$k, $v['mname']);
            $objActSheet->setCellValue('F'.$k, $v['create_time']);
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(25);
        }
        $width = array(10,20,60,10,15,20);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[0]);
        $objActSheet->getColumnDimension('B')->setWidth($width[3]);
        $objActSheet->getColumnDimension('C')->setWidth(50);
        $objActSheet->getColumnDimension('D')->setWidth($width[3]);
        $objActSheet->getColumnDimension('E')->setWidth($width[4]);
        $objActSheet->getColumnDimension('F')->setWidth(20);
        $outfile = "评论信息".date("Y-m-d H:i:s").".xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
    
    //导出信息流
    public function daochuxinxi()
    {
        $data=db('message')
            ->alias('me')
            ->join('f_user','me.uid=f_user.uid','left')
            ->join('f_real','f_user.uid=f_real.uid','left')
            ->order('me.mid desc')
            ->field('f_message.*,f_user.uname,f_user.nickname,f_user.uid,f_real.rflg')
            ->select();
        Vendor('phpexcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('phpexcel.PHPExcel.Worksheet.Drawing');
        Vendor('phpexcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H,I,J,K,L");
        $arrHeader =  array('序号','信息ID','类型','标题','供应数量','单位','价格','内容','用户','认证状态','发布时间','信息状态');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        $i=1;
        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$i++);
            $objActSheet->setCellValue('B'.$k, $v['mid']);
            if($v['type']=='1'){$v['type']='产品';}else{$v['type']='文章';}
            if($v['type']==2){$v['unit']='';}elseif($v['unit']=='1'){$v['unit']='吨';}elseif($v['unit']=='2'){$v['unit']='公斤';}elseif($v['unit']=='3'){$v['unit']='斤';}elseif($v['unit']=='4'){$v['unit']='个';}
            if($v['type']==2){$v['price']='';}elseif($v['price']!=''){$v['price']=$v['price'].'元/'.$v['unit'];}
            if($v['rflg']=='1'){$v['rflg']='已认证';}else{$v['rflg']='未认证';}
            if($v['stoptime']<date("Y-m-d H:i:s"))
            {
                $v['mflg']='过期';
            }
            else
            {
                if($v['mflg']=='0')
                {
                    $v['mflg']='锁定';
                }
                else
                {
                    $v['mflg']='正常';
                }
            }
            if($v['uname']==''){$v['uname']=$v['nickname'];}else{$v['uname']=$v['uname'];}
            $objActSheet->setCellValue('C'.$k,$v['type'] );
            /*$objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            print_r($v['picture']);
            $v[]=explode(",",$v['picture']);
            echo $v;die;
            $objDrawing[$k]->setPath('/upload/message/'.$v[0]['picture']);//这里拼接 . 是因为要在根目录下获取
            // 设置宽度高度
            $objDrawing[$k]->setHeight(80);//照片高度
            $objDrawing[$k]->setWidth(80); //照片宽度
            /*设置图片要插入的单元格*/
            //$objDrawing[$k]->setCoordinates('D'.$k);
            // 图片偏移距离
            //$objDrawing[$k]->setOffsetX(2);
           // $objDrawing[$k]->setOffsetY(12);
           // $objDrawing[$k]->setWorksheet($objExcel->getActiveSheet());*/

             //表格内容
            //$objActSheet->setCellValue('M'.$k,$v['picture'] );
            $objActSheet->setCellValue('D'.$k, $v['title']);
            $objActSheet->setCellValue('E'.$k, $v['number']);
            $objActSheet->setCellValue('F'.$k, $v['unit']);
            $objActSheet->setCellValue('G'.$k, $v['price']);
            $objActSheet->setCellValue('H'.$k, $v['content']);
            $objActSheet->setCellValue('I'.$k, $v['uname']);
            $objActSheet->setCellValue('J'.$k, $v['rflg']);
            $objActSheet->setCellValue('K'.$k, $v['createtime']);
            $objActSheet->setCellValue('L'.$k, $v['mflg']);
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(30);
        }

        $width = array(10,20,15,10,10,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[0]);
        $objActSheet->getColumnDimension('B')->setWidth($width[3]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth(30);
        $objActSheet->getColumnDimension('E')->setWidth($width[3]);
        $objActSheet->getColumnDimension('F')->setWidth($width[4]);
        $objActSheet->getColumnDimension('G')->setWidth(15);
        $objActSheet->getColumnDimension('H')->setWidth(80);
        $objActSheet->getColumnDimension('I')->setWidth($width[4]);
        $objActSheet->getColumnDimension('J')->setWidth($width[4]);
        $objActSheet->getColumnDimension('K')->setWidth(20);
        $objActSheet->getColumnDimension('L')->setWidth($width[4]);
        $outfile = "信息流".date("Y-m-d H:i:s").".xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }

    //导出拨号
    public function daochubohao()
    {
        $data=db('bohao')
            ->alias('bh')
            ->join('f_user us','bh.zjid=us.uid')
            ->join('f_user er','er.uid=bh.bjid')
            ->field('bh.*,us.uname as zjname,us.nickname as zjnickname,er.uname as bjname,er.nickname as bjnickname')
            ->order('bh.id desc')
            ->select();
        Vendor('phpexcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('phpexcel.PHPExcel.Worksheet.Drawing');
        Vendor('phpexcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F");
        $arrHeader =  array('序号','拨号ID','主叫用户','被叫用户','拨号时间');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        $i=1;
        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$i++);
            $objActSheet->setCellValue('B'.$k, $v['id']);
            // 表格内容
            if($v['zjname']==''){$v['zjname']=$v['zjnickname'];}else{$v['zjname']=$v['zjname'];}
            if($v['bjname']==''){$v['bjname']=$v['bjnickname'];}else{$v['bjname']=$v['bjname'];}
            $objActSheet->setCellValue('C'.$k, $v['zjname']);
            $objActSheet->setCellValue('D'.$k, $v['bjname']);
            $objActSheet->setCellValue('E'.$k, $v['b_createtime']);
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(25);
        }

        $width = array(10,20,15,10,10,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[0]);
        $objActSheet->getColumnDimension('B')->setWidth($width[3]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth($width[3]);
        $objActSheet->getColumnDimension('E')->setWidth(20);
        $outfile = "拨号".date("Y-m-d H:i:s").".xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }

    //导出转发
    public function daochuzhuanfa()
    {
        $data=db('zhuanfa')
            ->alias('zf')
            ->join('f_user us','zf.zfid=us.uid')
            ->join('f_user er','er.uid=zf.fbid')
            ->field('zf.*,us.uname as zfname,us.nickname as zfnickname,er.uname as fbname,er.nickname as fbnickname')
            ->order('zf.id desc')
            ->select();
        Vendor('phpexcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('phpexcel.PHPExcel.Worksheet.Drawing');
        Vendor('phpexcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel();
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F");
        $arrHeader =  array('序号','转发ID','转发人','信息ID','发布人','转发时间');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        $i=1;
        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$i++);
            $objActSheet->setCellValue('B'.$k, $v['id']);
            // // 图片生成
            // $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            // $objDrawing[$k]->setPath('public/static/admin/images/profile_small.jpg');
            // // 设置宽度高度
            // $objDrawing[$k]->setHeight(40);//照片高度
            // $objDrawing[$k]->setWidth(40); //照片宽度
            // /*设置图片要插入的单元格*/
            // $objDrawing[$k]->setCoordinates('C'.$k);
            // // 图片偏移距离
            // $objDrawing[$k]->setOffsetX(30);
            // $objDrawing[$k]->setOffsetY(12);
            // $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
            // 表格内容
            if($v['zfname']==''){$v['zfname']=$v['zfnickname'];}else{$v['zfname']=$v['zfname'];}
            if($v['fbname']==''){$v['fbname']=$v['fbnickname'];}else{$v['fbname']=$v['fbname'];}
            $objActSheet->setCellValue('C'.$k, $v['zfname']);
            $objActSheet->setCellValue('D'.$k, $v['mid']);
            $objActSheet->setCellValue('E'.$k, $v['fbname']);
            $objActSheet->setCellValue('F'.$k, $v['z_createtime']);
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(25);
        }

        $width = array(10,20,15,10,10,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[0]);
        $objActSheet->getColumnDimension('B')->setWidth($width[3]);
        $objActSheet->getColumnDimension('C')->setWidth($width[3]);
        $objActSheet->getColumnDimension('D')->setWidth($width[3]);
        $objActSheet->getColumnDimension('E')->setWidth($width[4]);
        $objActSheet->getColumnDimension('F')->setWidth(20);
        $outfile = "转发".date("Y-m-d H:i:s").".xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }

    //发现的显示页面
    public function faxian()
    {
        $rs=db('message')
            ->alias('me')
            ->join('f_user','me.uid=f_user.uid','left')
            ->join('f_real','f_user.uid=f_real.uid','left')
            ->order('me.state=1 DESC,me.paixu DESC')
            ->where('faxian',1)
            ->field('f_message.*,f_user.uname,f_user.nickname,f_user.uid,f_real.rflg')
            ->paginate(8);
        $page=$rs->render();
        //print_r($rs);die();
        $tiao=db('message')->where('faxian',1)->count();
        return view('faxian',['rs'=>$rs,'page'=>$page,'tiao'=>$tiao]);
    }

    //置顶状态的改变
    public function faxianstate()
    {
        $mid = input('mid');
        $cha=db('message')->where('mid',$mid)->field('state,paixu,keep')->find();
        if($cha['state']==1)
        {
            $state=0;
            $paixu=db('message')->where('mid',$mid)->update(['paixu'=>$cha['keep']]);
        }
        else
        {
            $state=1;
            $max_paixu=db("message")->where("faxian",1)->max("paixu");
            $shu=$max_paixu+1;
            $paixu=db('message')->where('mid',$mid)->update(['paixu'=>$shu,'keep'=>$cha['paixu']]);
        }
        // print_r($mid);die();
        $rs=db('message')->where('mid',$mid)->update(['state'=>$state]);
        if($rs)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //上移状态的改变
    public function shangyi()
    {
        $mid=input('mid');
        $rs=db('message')
            ->where('mid',$mid)
            ->field('paixu,state')
            ->find();
        $max_sort = db("message")
            ->where("faxian",1)
            ->where("state",0)
            ->max("paixu");
        if($rs['state']=='1')
        {
            $data['code']=0;
            $data['msg']='置顶作品不可操作！';
            return $data;
        }
        if($rs['paixu'] == $max_sort){
            $data['code']=0;
            $data['msg']='已经到达限制！';
        }else{
            $cha=db('message')
                ->where('paixu','>',$rs['paixu'])
                ->where('faxian',1)
                ->where("state",0)
                ->order('f_message.paixu asc')
                ->limit(0,1)
                ->find();
            $shang=db('message')
                ->where('mid',$mid)
                ->update(['paixu'=>$cha['paixu']]);
            $xia=db('message')
                ->where('mid',$cha['mid'])
                ->update(['paixu'=>$rs['paixu']]);
            if($shang&&$xia)
            {
                db::commit();
                $data['code']=1;
                $data['msg']='上移成功！';
            }
            else
            {
                db::rollback();
                $data['code']=0;
                $data['msg']='上移错误！';

            }
        }
        return $data;

    }

//下移状态的改变
    public function xiayi()
    {
        $mid=input('mid');
        $rs=db('message')
            ->where('mid',$mid)
            ->field('paixu,state')
            ->find();
        $max_sort = db("message")
            ->where("faxian",1)
            ->where("state",0)
            ->min("paixu");
        if($rs['state']=='1')
        {
            $data['code']=0;
            $data['msg']='置顶作品不可操作！';
            return $data;
        }
        if($rs['paixu'] == $max_sort){
            $data['code']=0;
            $data['msg']='已经到达最低！';
        }else {
            $cha = db('message')
                ->where('paixu', '<', $rs['paixu'])
                ->where('faxian', 1)
                ->where("state",0)
                ->order('f_message.paixu desc')
                ->limit(0, 1)
                ->find();
            //return $rs['paixu'];
            $xia = db('message')
                ->where('mid', $mid)
                ->update(['paixu' => $cha['paixu']]);
            $shang = db('message')
                ->where('mid', $cha['mid'])
                ->update(['paixu' => $rs['paixu']]);
            if ($shang && $xia) {
                db::commit();
                $data['code'] = 1;
                $data['msg'] = '下移成功！';
            } else {
                db::rollback();
                $data['code'] = 0;
                $data['msg'] = '下移错误！';
            }
        }
        return $data;
    }

    //信息流的所有显示
    public function xinxi()
    {
        //sql语句，定义为全局变量
        $rs=db('message')
            ->alias('me')
            ->join('f_user','me.uid=f_user.uid','left')
            ->join('f_real','f_user.uid=f_real.uid','left')
            ->order('me.mid desc')
            ->field('f_message.*,f_user.uname,f_user.nickname,f_user.uid,f_real.rflg');
        //判断是否点击和显示内容
        if(input('sub'))
        {
            $cateid=input('cateid','');//下拉菜单
            $key=input('Key');//输入框
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
            //判断$key是不是空
            //判断时间
            if(isset($datemin) && $datemin != ''){
                $rs = $rs->whereTime('f_message.createtime', '>=', input('datemin').' 00:00:00');
                //print_r($rs);die;
            }
            if(isset($datemax) && $datemax != ''){
                $rs = $rs->whereTime('f_message.createtime', '<=', input('datemax').' 23:59:59');
            }
          	if($cateid=='')
            {
                $rs=$rs->paginate(7,false,['query'=>request()->param()]);
                $page=$rs->render();
                return view('xinxi',['rs'=>$rs,'page'=>$page]);
            }
            if($key=='')
            {
                $rs=$rs->paginate(7,false,['query'=>request()->param()]);
                $page=$rs->render();
                return view('xinxi',['rs'=>$rs,'page'=>$page]);
            }
            else
            {
                if($key=='正常')
                {
                    $key='1';
                }
                if($key=='锁定')
                {
                    $key='0';
                }
                if($key=='过期')
                {
                    //$where=array();
                    $wh['f_message.stoptime']=array("elt",date("Y-m-d H:i:s"));
                    $rs=$rs->where($wh)->paginate(7,false,['query'=>request()->param()]);
                    $page=$rs->render();
                    return view('xinxi',['rs'=>$rs,'page'=>$page]);
                }

                //判断输入内容
                switch ($cateid){
                    case 'title':$date=['title'=>$key];break;//标题
                    case 'mid':$date=['mid'=>$key];break;//信息id
                    case 'mflg':$date=['mflg'=>$key];break;//信息状态
                    case 'uflg':$date=['uflg'=>$key];break;//用户状态
                    //case 'stoptime';$date=['stoptime'=>$key];break;//过期时间
                }
                $rs=$rs->where($cateid,'like','%'.$key.'%')->paginate(7,false,['query'=>request()->param()]);
                $page=$rs->render();
                return view('xinxi',['rs'=>$rs,'page'=>$page]);
            }

        }else{
            //$arr=array();
            //$arr=$rs->toArray();
            /*foreach ($rs as $k)
            {
                $arr[]=$k->toArray();
            }*/
            //print_r($rs);die;
            /*foreach ($rs as $key=>$value)
            {
                //echo $rs[$key]['mflg'];
                if($value['stoptime']<date("Y-m-d H:i:s"))
                {
                    $rs[$key]['mflg']= '过期';
                }
                else
                {
                    if($value['mflg']=='0')
                    {
                        $rs[$key]['mflg'] ='锁定';
                    }
                    else
                    {
                         echo $rs[$key]['mflg'];
                         $rs[$key]['mflg'] = '正常';
                    }
                }
            }*/
            $rs=$rs->paginate(7);
            $page=$rs->render();
            return view('xinxi',['rs'=>$rs,'page'=>$page]);
        }
    }

    //详细信息的查看
    public function chakan()
    {
        if(request()->isAjax())
        {
            $mid = input('mid');
            //return view('chakan');
        }
        else
        {
            $mid = input('mid');
            //echo $mid;die();
            $rs=db('message')
                ->alias('me')
                ->join('f_user us','us.uid=me.uid','left')
                ->join('f_real re','re.uid=us.uid','left')
                ->join('f_comments co','co.mid=me.mid','left')
                ->where('me.mid',$mid)
                ->field('me.*,us.phone,us.uname,us.nickname,us.uflg,co.sid,co.plid,co.ccontent,co.ccreatetime,co.sflg')
                ->find();
            $st=db('message')->where('mid',$mid)->field('id')->select();
            foreach($st as $key){
                foreach($key as $val){
                    $id=$val;
                }
            }
            $arr=explode(",",$id);
            for($i=0;$i<count($arr);$i++)
            {
                $res=db('user')->where('uid',$arr[$i])->field('uname,nickname')->find();
                if($res['uname']=='')
                {
                    $ast[$i]=$res['nickname'];
                }
                else
                {
                    $ast[$i]=$res['uname'];
                }

            }
            //查看的评论部分
            //$mid = input('mid');
            //echo $mid;die();
            $ar=$this->evalutelist($mid);
            //print_r($ar);die;
            for($i=0;$i<count($ar);$i++)
            {
                $res=db('user')->where('uid',$ar[$i]['pjid'])->field('uname,nickname')->find();
                $ar[$i]['pjid']=$res['uname'];
                $ar[$i]['pnickname']=$res['nickname'];
            }

            //评论表的实现
            //图片转化未数组
          	$arr2=array();
            $arr2=array_filter(explode(",",$rs['picture']));
          	//判断照片是否为空
          	if(empty($arr2))
            {
            	$arr2='kong';
            }
            //print_r($arr2);die;
            return view('chakan',['rs'=>$rs,'res'=>$ast,'arr2'=>$arr2,'evalute'=>$ar]);
        }
    }
    public function evalutelist($message_id)
    {
        //$message_id=input('message_id');
        $evalutelist=Db('evalute')
            ->where('message_id',$message_id)
            ->where('parent_evalute_id',0)
            ->select();
        $arr=$evalutelist;
        if(!empty($evalutelist)){
            foreach($evalutelist as $k=>$v){
                $downevalute=Db('evalute')->where('message_id',$message_id)->where('parent_evalute_id',$v['evalute_id'])->count();
                if($downevalute>0){
                    $arr[$k]['down']=$this->getdownevalutes(array(), $message_id, $v['evalute_id']);
                }else{
                    $arr[$k]['down']=array();
                }
            }
        }return $arr;
    }
    public function getdownevalutes($arr,$message_id,$parent_evalute_id)
    {
        $downevalute=Db('evalute')
            ->where('message_id',$message_id)
            ->where('parent_evalute_id',$parent_evalute_id)
            ->count();
        if($downevalute>0) {
            $evalute = Db('evalute')
                ->join('f_user u','f_evalute.pjid=u.uid')
                ->join('f_user s','f_evalute.bpid=s.uid')
                ->where('message_id', $message_id)
                ->where('parent_evalute_id',$parent_evalute_id)
                ->field('f_evalute.*,s.uname as sname,s.nickname as snickname,u.uname as uname,u.nickname as unickname')
                ->select();
            foreach ($evalute as $k) {
                $arr[] = $k;
                $arr = $this->getdownevalutes($arr, $message_id, $k['evalute_id']);
            }
        }
        return $arr;
    }

    public function getdownevalute($sid,$arr=array(),$mid)
    {
        if(empty($mid)){
            return array();
        }
        $down=Db('comments')->where('bid',$sid)
            ->where('mid',$mid)
            ->select();
        //print_r($down);die
        for($i=0;$i<count($arr);$i++){
            $name=db('comments')
                ->join('f_user','f_user.uid=f_comments.plid')
                ->where('uid',$arr[$i]['plid'])
                ->select();

            $name= $name['0']['uname'];
            $arr[$i]['plid']=$name;
        }

        //dump($down);
        if(empty($down)){
            return $down;
        }
        $array = $arr;
        foreach ($down as $k => $v){

            $v['son'] = $this->getdownevalute($v['sid'],$array, $mid);
            $array[] = $v;
        }
        return $array;
    }

    //是否在发现中显示的状态修改
    public function faxianflg()
    {
        $r = array();
        $mid = input('mid');
        $cha=db('message')->where('mid',$mid)->field('faxian,paixu')->find();
        if($cha['faxian']==1)
        {
            $faxian=0;
            //取消置顶
            $no=db('message')->where('mid',$mid)->update(['state'=>0]);
        }
        else
        {
            $faxian=1;
            $max_sort = db("message")->where("faxian",1)->max("paixu");
            if($max_sort == ''){
                if($cha['paixu'] != 1) {
                    $r [] = db('message')
                        ->where('mid', $mid)
                        ->update(['paixu' => 1]);
                }
            }else{
                //if($cha['paixu'] != $max_sort+1) {
                    $r [] = db('message')
                        ->where('mid', $mid)
                        ->update(['paixu' => $max_sort + 1]);
                //}
            }
        }
        $r [] =db('message')->where('mid',$mid)->update(['faxian'=>$faxian]);
        if(!in_array(false,$r,true))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //是否在发现中显示黑名单的修改
    public function suodingflg()
    {
        $mid = input('mid');
        $cha=db('message')->where('mid',$mid)->field('mflg')->find();
        if($cha['mflg']==1)
        {
            $mflg=0;
        }
        else
        {
            $mflg=1;
        }
        // print_r($mid);die();
        $rs=db('message')->where('mid',$mid)->update(['mflg'=>$mflg]);
        if($rs)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //评论状态的修改
    public function pinglunflg()
    {
        $sid = input('sid');
        $cha=db('evalute')->where('evalute_id',$sid)->field('sfig')->find();
        if($cha['sfig']==1)
        {
            $sfig=2;
        }
        else
        {
            $sfig=1;
        }
        // print_r($mid);die();
        $rs=db('evalute')->where('evalute_id',$sid)->update(['sfig'=>$sfig]);
        if($rs)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //撤销拉黑状态改变
    public function chexiaolahei()
    {
        $mid = input('mid');
        $mflg=1;
        $rs=db('message')->where('mid',$mid)->update(['mflg'=>$mflg]);
        if($rs)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //垃圾信息的显示
    public function laji()
    {
        $rs=db('message')
            ->alias('me')
            ->join('f_user','me.uid=f_user.uid','left')
            ->join('f_real','f_user.uid=f_real.uid','left')
            ->order('me.mid desc')
            ->where('mflg',0)
            ->field('f_message.*,f_user.uname,f_user.nickname,f_user.uid,f_real.rflg')
            ->paginate(7);
        $page=$rs->render();
        //print_r($rs);die();
        return view('laji',['rs'=>$rs,'page'=>$page]);
    }

    //评论显示
    public function pinglun()
    {
        $where = array();
//判断是否点击和显示内容
        if(input('sub'))
        {
            $cateid=input('cateid','');//下拉菜单
            $key=trim(input('name'));//输入框
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
            //判断$key是不是空
            //判断时间
            if(isset($datemin) && $datemin != ''){
                $where['f_evalute.create_time'] = array("egt",input('datemin').' 00:00:00');
                //$rs = $rs->whereTime('f_comments.ccreatetime', '>=', input('datemin').' 00:00:00');
                //print_r($rs);die;
            }
            if(isset($datemax) && $datemax != ''){
                $where['f_evalute.create_time'] = array("elt",input('datemax').' 23:59:59');
                //$rs = $rs->whereTime('f_comments.ccreatetime', '<=', input('datemax').' 23:59:59');
            }
            //dump($_POST);die;
            if($key=='')
            {
                //echo 11;die;
                $rs=db('evalute')
                    ->alias('ev')
                    ->join('f_message me','me.mid=ev.message_id')
                    ->join('f_user us','us.uid=me.uid')
                    ->field('ev.*,me.uid,us.uname,us.nickname')->where($where)->paginate("11",false,['query' => request()->param()]);
                $items = $rs->items();
                //print_r($items);die;
                foreach ($items as $k => $v){
                    if($v['bpid'] == 0){
                        $plname = db('user')->where('uid',$v['pjid'])->field('uname,uid,nickname')->find();
                        $items[$k]['pl_name']=$plname['uname'];
                        $items[$k]['pl_nickname']=$plname['nickname'];
                        $items[$k]['u_uid']=$plname['uid'];
                    }else{
                        $plname = db('user')->where('uid',$v['pjid'])->field('uname,uid,nickname')->find();
                        $as=db('user')->alias("u")
                            ->join("f_evalute c","u.uid = c.pjid")
                            ->where('evalute_id',$v['bpid'] )->field('u.uname,u.uid,u.nickname')->find();
                        $items[$k]['hf_name']=$as['uname'];
                        $items[$k]['hf_nickname']=$as['nickname'];
                        $items[$k]['hf_uid']=$as['uid'];
                        $items[$k]['pl_name']=$plname['uname'];
                        $items[$k]['pl_nickname']=$plname['nickname'];
                        $items[$k]['u_uid']=$plname['uid'];
                    }
                }

                $page=$rs->render();
                return view('pinglun',['rs'=>$items,'page'=>$page]);
                //return view('pinglun',['rs'=>$rs]);
            }
            else
            {
                //判断输入内容
                switch ($cateid){
                    case 'uname':
                        $date=['uname'=>$key];
                        //dump($date['uname']);die;
                        $id=db('user')->alias('us')->where('uname|nickname','like',"%".$date['uname']."%")->find();
                        //print_r($id);die;
                        //$cha=db('comments')->alias('cc')->where('uid',$id['uid'])->find();
                        $where ['ev.pjid'] = array("eq",$id['uid']);
                        //$where ['ev.bpid'] = array("eq",$id['uid']);
                        break;//用户
                    case 'evalute_id':
                        $date=['evalute_id'=>$key];
                        $where ['ev.evalute_id'] = array("eq",$key);
                        break;//评论id
                    case 'txt':
                        $date=['txt'=>$key];
                        $where ['ev.txt'] = array("like","%".$key."%");
                        break;//内容
                    default:
                        $where ['ev.txt'] = array("like","%".$key."%");
                        break;
                }
            }

        }
       $rs=db('evalute')
            ->alias('ev')
            ->join('f_message me','me.mid=ev.message_id')
            ->join('f_user us','us.uid=me.uid')
            ->field('ev.*,me.uid,us.uname,us.nickname')->where($where)->paginate("11",false,['query' => request()->param()]);
        $items = $rs->items();
        foreach ($items as $k => $v){
            //print_r($v);
            if($v['bpid']==0){
                $plname = db('user')->where('uid',$v['pjid'])->field('uname,uid,nickname')->find();
                $items[$k]['pl_name']=$plname['uname'];
                $items[$k]['pl_nickname']=$plname['nickname'];
                $items[$k]['u_uid']=$plname['uid'];
            }else{
                $plname = db('user')->where('uid',$v['pjid'])->field('uname,uid,nickname')->find();
                $as=db('user')->alias("u")
                    ->join("f_evalute c","u.uid = c.pjid")
                    ->where('evalute_id',$v['bpid'] )->field('u.uname,u.uid,u.nickname')->find();
                $items[$k]['hf_name']=$as['uname'];
                $items[$k]['hf_nickname']=$as['nickname'];
                $items[$k]['hf_uid']=$as['uid'];
                $items[$k]['pl_name']=$plname['uname'];
                $items[$k]['pl_nickname']=$plname['nickname'];
                $items[$k]['u_uid']=$plname['uid'];
            }
        }
        $page=$rs->render();
        return view('pinglun',['rs'=>$items,'page'=>$page]);

    }

    public function add()
    {
        if(request()->isAjax()){
            $title=input('title');
            $number=input('number');
            $unit=input('unit');
            $qixian=input('qixian');
            $price=input('price');
            $content=input('content');
            $address=input('address');
            $picture=input('picture');
            $type=1;//type产品类型为产品代号为数字1
            date_default_timezone_set('PRC');//获取当前时间
            $time=date('Y-m-d H:i:s');
            $createtime=$time;
            $mflg=1;//信息状态正常
            $uid=input('uid');//获取到用户的uid
            $state=0;//未置顶的状态
            $id='';//点赞人数为空
            $s=(($qixian)*3600*24);
            $tx=time();
            $bb=($s+$tx);
            $stoptime=date('Y-m-d H:i:s',$bb);//此时间为发布时长计算后填写的时间
            $timelimit=$qixian;//有效期
            $faxian=0;//默认不在发现显示
            $tianjia=db('message')
                ->insert([
                    'type'=>$type,
                    'picture'=>$picture,
                    'title'=>$title,
                    'number'=>$number,
                    'price'=>$price,
                    'content'=>$content,
                    'createtime'=>$createtime,
                    'mflg'=>$mflg,
                    'unit'=>$unit,
                    'uid'=>$uid,
                    'id'=>$id,
                    'state'=>$state,
                    'stoptime'=>$stoptime,
                    'faxian'=>$faxian,
                    'address'=>$address,
                    'timelimit'=>$timelimit]);

            if($tianjia>0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }else
        {
            $rs=db('user')->field('uid,uname')->select();
            return view('chanpingadd',['rs'=>$rs]);
        }
    }

    //文章添加
    public function wenzhangadd()
    {
        if(request()->isAjax()){
            $title=input('title');
            //$number=input('number');
            //$unit=input('unit');
            //$qixian=input('qixian');
            //$price=input('price');
            $content=input('content');
            $address=input('address');
            $picture=input('picture');
            $type=2;//type产品类型为产品代号为数字1
            date_default_timezone_set('PRC');//获取当前时间
            $time=date('Y-m-d H:i:s');
            $createtime=$time;
            $mflg=1;//信息状态正常
            $uid=input('uid');//获取到用户的uid
            $state=0;//未置顶的状态
            $id='';//点赞人数为空
            //$s=(($qixian)*3600*24);
            //$tx=time();
            //$bb=($s+$tx);
            //$stoptime=date('Y-m-d H:i:s',$bb);//此时间为发布时长计算后填写的时间
           // $timelimit=$qixian;//有效期
            $faxian=0;//默认不在发现显示
            $tianjia=db('message')
                ->insert([
                    'type'=>$type,
                    'picture'=>$picture,
                    'title'=>$title,
                    'number'=>'',
                    'price'=>'',
                    'content'=>$content,
                    'createtime'=>$createtime,
                    'mflg'=>$mflg,
                    'unit'=>'',
                    'uid'=>$uid,
                    'id'=>$id,
                    'state'=>$state,
                    'stoptime'=>'3009:12:20',
                    'faxian'=>$faxian,
                    'address'=>$address,
                    'timelimit'=>'']);

            if($tianjia>0)
            {
                return array('code'=>1,'msg'=>'发布成功！');
                //return 1;
            }
            else
            {
                return array('code'=>0,'msg'=>'发布失败！');
            }
        }else
        {
            /*$uid=input('uid');//获取到用户的uid
            $timestart=date("Y-m-d 00:00:00");
            $timeend=date("Y-m-d H:i:s");

            $shu=db('message')
                ->where('uid',$uid)
                ->where('f_message.createtime','between',[$timestart,$timeend])
                ->count();//今日实名
            if($shu>2)
            {
                return array('code'=>0,'msg'=>'普通！');
            }*/
            $rs=db('user')->field('uid,uname')->select();
            return view('wenzhangadd',['rs'=>$rs]);
        }
    }

    public function doUpload()
    {
        $file = request()->file('file');
        //echo $file;die;
        $fileName=md5(uniqid(md5(microtime(true)),true));
        $imgUrl= ROOT_PATH . '/upload/message/';
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move($imgUrl,$fileName);
        $error = $file->getError();
        if ($info) {
            // 成功上传后 获取上传信息
            $url= $info->getSaveName();
            return json(['code' => 1, 'msg' => '上传成功!', 'url' =>$url]);
        } else {
            // 上传失败获取错误信息
            return json(['code' => 0, 'msg' => $file->getError(), 'url' => '']);
        }

    }

    //拨号的显示
    public function bohaoindex()
    {
        $rs=db('bohao')
            ->alias('bh')
            ->join('f_user us','bh.zjid=us.uid')
            ->join('f_user er','er.uid=bh.bjid')
            ->field('bh.*,us.uname as zjname,us.nickname as znickname,er.uname as bjname,er.nickname as bnickname')
            ->order('bh.id desc')
            ->paginate(15);
        $page=$rs->render();
       // print_r($rs);die();
        $tiao=db('bohao')->count();
        return view('bohao',['rs'=>$rs,'page'=>$page,'tiao'=>$tiao]);
    }

    //拨号的添加
    public function bohaoadd()
    {
        if(request()->isAjax())
        {
            $zhujiao=input('zhujiao');
            $beijiao=input('beijiao');
            date_default_timezone_set('PRC');//获取当前时间
            $time=date('Y-m-d H:i:s');
            $b_createtime=$time;
            $tianjia=db('bohao')
                ->insert([
                    'zjid'=>$zhujiao,
                    'bjid'=>$beijiao,
                    'b_createtime'=>$b_createtime]);
            if($tianjia)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            $rs=db('user')->field('f_user.uid,f_user.uname')->select();
            $res=db('user')->field('f_user.uid,f_user.uname')->select();
            //print_r($rs);die();
            return view('bohaoadd',['rs'=>$rs,'res'=>$res]);
        }
    }

    //转发的显示
    public function zhuanfaindex()
    {
        $rs=db('zhuanfa')
            ->alias('zf')
            ->join('f_user us','zf.zfid=us.uid')
            ->join('f_user er','er.uid=zf.fbid')
            ->field('zf.*,us.uname as zfname,us.nickname as znickname,er.uname as fbname,er.nickname as fnickname')
            ->order('zf.id desc')
            ->paginate(15);
        $page=$rs->render();
        //print_r($rs);die();
        $tiao=db('zhuanfa')->count();
        return view('zhuanfa',['rs'=>$rs,'page'=>$page,'tiao'=>$tiao]);
    }

    //转发的添加
    public function zhuanfaadd()
    {
        if(request()->isAjax())
        {
            //$biao=input('biaoqing');
            /*$strEncode = '';
            $length = mb_strlen($biao,'utf-8');
            for ($i=0; $i < $length; $i++) {
                $_tmpStr = mb_substr($biao,$i,1,'utf-8');
                if(strlen($_tmpStr) >= 4){
                    $strEncode .= rawurlencode($_tmpStr);
                }else{
                    $strEncode .= $_tmpStr;
                }
            }
            return $strEncode;die;*/
            $zhuanfa=input('zhuanfa');
            $wz=input('wenzhang');
            $cha=db('message')->where('mid',$wz)->find();
            $uid=$cha['uid'];
            date_default_timezone_set('PRC');//获取当前时间
            $time=date('Y-m-d H:i:s');
            $b_createtime=$time;
            $tianjia=db('zhuanfa')
                ->insert([
                    'zfid'=>$zhuanfa,
                    'fbid'=>$uid,
                    'mid'=>$wz,
                    'z_createtime'=>$b_createtime]);
            if($tianjia)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            $rs=db('user')->field('f_user.uid,f_user.uname')->select();
            $res=db('user')->field('f_user.uid,f_user.uname')->select();
            $wenzahng=db('message')
                ->alias('me')
                ->field('me.uid,me.mid')
                ->select();
            //print_r($rs);die();
            return view('zhuanfaadd',['rs'=>$rs,'res'=>$res,'wz'=>$wenzahng]);
        }
    }

    //信息的设置
    public function shezhi()
    {
        if(request()->isAjax())
        {
            $userbohao=input('userbohao');
            $vipbohao=input('vipbohao');
            $vipwenzhang=input('vipwenzhang');
            $userwenzhang=input('userwenzhang');
            $userzhuanfa=input('userzhuanfa');
            $vipzhuanfa=input('vipzhuanfa');
            $tianjia=db('shezhi')
                ->where('zid',1)
                ->update([
                    'userbohao'=>$userbohao,
                    'vipbohao'=>$vipbohao,
                    'vipwenzhang'=>$vipwenzhang,
                    'userwenzhang'=>$userwenzhang,
                    'userzhuanfa'=>$userzhuanfa,
                    'vipzhuanfa'=>$vipzhuanfa]);
            if($tianjia)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            $rs=db('shezhi')->where('zid',1)->find();
            return view('shezhi',['rs'=>$rs]);
        }
    }

}